﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Admin.Master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    OpenCategoryPopup
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <%Html.RenderPartial("_PopupCategory"); %>
    <script type="text/javascript" charset="utf-8">
        function SearchCategoryName() {
            removeAllInLeft();
            var CategoryName = $("#txtCategoryName").val();
            var All = $("#AllCategoryList option");
            var FinalLeft = $("#LeftCategoryList");
            var numberAll = parseInt(All.length);
            for (var i = 0; i < numberAll; i++) {
                var content = All[i].innerText;
                if (content == undefined) {
                    content = All[i].textContent
                }
                if (content.toLowerCase().indexOf(CategoryName.toLowerCase()) != -1) {
                    FinalLeft.append('<option value=' + All[i].getAttribute('value') + '>' + content + '</option>');
                }
            }
        }
        function removeAllInLeft() {
            var Left = $("#LeftCategoryList option");
            Left.remove();
        }
        function AddCategory() {
            var CategoryName = $("#txtCategoryName").val();
            var href = "/Category/DoCreateCategory";
            $.post(href, { CategoryName: CategoryName },
           function (data) {
               alert(data);
           });
        }
        $('#LeftCategoryList').dblclick(function () {
            SaveCategory();
        });

        function LeftToRight() {
            var Left = $("#LeftCategoryList option:selected");
            var Right = $("#RightCategoryList option");
            var FinalRight = $("#RightCategoryList");
            var numberSelected = parseInt(Left.length);
            var NumberRowInRight = parseInt(Right.length);
            for (var i = 0; i < numberSelected; i++) {
                var duplicate = 0;
                for (var j = 0; j < NumberRowInRight; j++) {
                    if (Right[j].getAttribute("value") == Left[i].getAttribute("value")) {
                        duplicate = 1;
                    }
                }
                if (duplicate == 0) {
                    var content = Left[i].innerText;
                    if (content == undefined) {
                        content = Left[i].textContent
                    }

                    FinalRight.append('<option value=' + Left[i].getAttribute('value') + '>' + content + '</option>');
                }
            }
        }
        function RemoveFromRight() {
            var Right = $("#RightCategoryList option:selected");
            var numberSelected = parseInt(Right.length);
            for (var i = 0; i < numberSelected; i++) {
                $("#RightCategoryList option[value=" + Right[i].getAttribute('value') + "]").replaceWith("");
            }
        }
        function SaveCategory() {
            //step 1: get Category in Left Div
           
            var Category = "";
            var Left = $("#LeftCategoryList option:selected");
            var numberSelected = parseInt(Left.length);
            for (var i = 0; i < numberSelected; i++) {
                var content = Left[i].innerText;
                if (content == undefined) {
                    content = Left[i].textContent
                }
                Category += "," + Left[i].getAttribute('value');
                Category += "!" + content;
            }
          
            window.opener.loadDivCategory(Category);
            window.close();


        }

    </script>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="Navigation" runat="server">
</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="MenuLeft" runat="server">
</asp:Content>
<asp:Content ID="Content5" ContentPlaceHolderID="Toolbar" runat="server">
</asp:Content>
